package cn.melonlib.cores.graphql.config.bean.impl;

import cn.melonlib.cores.commons.comp.utils.EntityWrapperContainer;
import cn.melonlib.cores.commons.model.entity.IEntity;
import cn.melonlib.cores.graphql.config.bean.ParameterReader;
import cn.melonlib.cores.jpa.model.bean.collections.JsonMap;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

@Component
public class EntityReader<ENTITY extends IEntity> implements ParameterReader<ENTITY> {

    @Resource
    private EntityWrapperContainer entityWrapperContainer;

    public ENTITY read(String fieldName, DataFetchingEnvironment env,Class<ENTITY> cls) throws Exception{
        Map<String,Object> map=env.getArgument(fieldName);
        if(map==null){
            return null;
        }
        return (ENTITY) entityWrapperContainer.wrap(EntityWrapperContainer.JSON, JsonMap.createMap(map).jsonString(), cls);
    }
}
